//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.pt;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.PtCoachCodeDao;
import com.aidong.dao.wills.PtContractDao;
import com.aidong.dao.wills.PtItemCodeDao;
import com.aidong.dao.wills.PtPriceDao;
import com.aidong.model.wills.ContractInfo;
import com.aidong.model.wills.MemberEarnest;
import com.aidong.model.wills.PayList;
import com.aidong.model.wills.PtCoachCode;
import com.aidong.model.wills.PtContract;
import com.aidong.model.wills.PtItemCode;
import com.aidong.model.wills.PtPrice;
import com.aidong.util.ExportXls;
import com.aidong.web.controller.member.PayController;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"ptContract"})
public class PtContractController extends PayController {
    @Resource
    PtContractDao ptContractDao;
    @Resource
    PtItemCodeDao ptItemCodeDao;
    @Resource
    PtCoachCodeDao ptCoachCodeDao;
    @Resource
    PtPriceDao ptPriceDao;

    public PtContractController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return (new ModelAndView("pt/contract/list")).addObject("shopName", this.getTypeList("shopName")).addObject("saleType", this.getTypeList("ptSaleType")).addObject("itemType", this.getTypeList("ptItemType")).addObject("dt1", sdf.format(new Date(System.currentTimeMillis() - 86400000L))).addObject("dt2", sdf.format(new Date()));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "saleType",required = false) String saleType, @RequestParam(value = "itemType",required = false) String itemType, @RequestParam(value = "contractCode",required = false) String contractCode, @RequestParam(value = "memberCode",required = false) String memberCode, @RequestParam(value = "cardCode",required = false) String cardCode, @RequestParam(value = "salesCode",required = false) String salesCode, @RequestParam(value = "dateType",required = false) String dateType, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "c.shopCode", shopCode);
        cd.eq("c.saleType", saleType);
        cd.eq("c.contractCode", contractCode);
        cd.eq("c.memberCode", memberCode);
        cd.eq("m.cardCode", cardCode);
        cd.eq("i.itemType", itemType);
        ConditionMap mv;
        if(!this.chkNull(salesCode)) {
            mv = new ConditionMap(1);
            mv.eq("c.salesCode", salesCode);
            mv.eq("c.salesCode2", salesCode);
            cd.addSub(mv);
        }

        if(this.queryOwn()) {
            mv = new ConditionMap(1);
            mv.eq("c.salesCode", this.getLoginUserCode());
            mv.eq("c.salesCode2", this.getLoginUserCode());
            mv.eq("c.createUser", this.getLoginUserCode());
            cd.addSub(mv);
        }

        if("2".equals(dateType)) {
            if(!this.chkNull(dateFrom)) {
                cd.ge("c.commissionDate", this.getDateFrom(dateFrom));
            }

            if(!this.chkNull(dateTo)) {
                cd.le("c.commissionDate", this.getDateTo(dateTo));
            }
        } else {
            if(!this.chkNull(dateFrom)) {
                cd.ge("c.saleDate", this.getDateFrom(dateFrom));
            }

            if(!this.chkNull(dateTo)) {
                cd.le("c.saleDate", this.getDateTo(dateTo));
            }
        }

        this.ptContractDao.queryPageList(pg, cd);
        ModelAndView mv1 = new ModelAndView("pt/contract/_list");
        mv1.addObject("list", pg.getResultList());
        mv1.addObject("pg", pg);
        return mv1;
    }

    @RequestMapping({"expt"})
    public ModelAndView expt(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "saleType",required = false) String saleType, @RequestParam(value = "itemType",required = false) String itemType, @RequestParam(value = "contractCode",required = false) String contractCode, @RequestParam(value = "memberCode",required = false) String memberCode, @RequestParam(value = "cardCode",required = false) String cardCode, @RequestParam(value = "salesCode",required = false) String salesCode, @RequestParam(value = "dateType",required = false) String dateType, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, HttpServletResponse response) {
        try {
            Pagination e = new Pagination(1, '\uea60');
            e.setPage(Integer.valueOf(1), Integer.valueOf(1));
            ConditionMap cd = new ConditionMap();
            this.addConditionShopCode(cd, "c.shopCode", shopCode);
            cd.eq("c.saleType", saleType);
            cd.eq("c.contractCode", contractCode);
            cd.eq("c.memberCode", memberCode);
            cd.eq("m.cardCode", cardCode);
            cd.eq("i.itemType", itemType);
            ConditionMap xls;
            if(!this.chkNull(salesCode)) {
                xls = new ConditionMap(1);
                xls.eq("c.salesCode", salesCode);
                xls.eq("c.salesCode2", salesCode);
                cd.addSub(xls);
            }

            if(this.queryOwn()) {
                xls = new ConditionMap(1);
                xls.eq("c.salesCode", this.getLoginUserCode());
                xls.eq("c.salesCode2", this.getLoginUserCode());
                xls.eq("c.createUser", this.getLoginUserCode());
                cd.addSub(xls);
            }

            if("2".equals(dateType)) {
                if(!this.chkNull(dateFrom)) {
                    cd.ge("c.commissionDate", this.getDateFrom(dateFrom));
                }

                if(!this.chkNull(dateTo)) {
                    cd.le("c.commissionDate", this.getDateTo(dateTo));
                }
            } else {
                if(!this.chkNull(dateFrom)) {
                    cd.ge("c.saleDate", this.getDateFrom(dateFrom));
                }

                if(!this.chkNull(dateTo)) {
                    cd.le("c.saleDate", this.getDateTo(dateTo));
                }
            }

            this.ptContractDao.queryPageList(e, cd);
            ExportXls xls1 = new ExportXls(response, "私教销售流水", "私教销售流水");
            xls1.addHead(new String[]{"门店编号", "门店名称", "合同号", "销售类别", "销售日期", "有效期始日", "有效期迄日", "卡号", "会员号", "会员姓名", "课程编号", "课程名称", "教练编号", "教练姓名", "课时数", "金额", "收款金额", "定金转入", "销售员", "销售员2", "操作员", "原合同号", "备注", "合同上交", "内审状态", "内审时间", "延期定金"});
            xls1.setWidth(new int[]{10, 10, 15, 10, 20, 12, 12, 12, 20, 10, 10, 20, 10, 10, 10, 10, 10, 10, 15, 15, 15, 15, 20, 10, 10, 12});
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Iterator i$ = e.getResultList().iterator();

            while(i$.hasNext()) {
                ContractInfo contract = (ContractInfo)i$.next();
                xls1.addRow(new Object[]{contract.getShopCode(), this.cache.getValue("shopName", contract.getShopCode()), contract.getContractCode(), this.cache.getValue("ptSaleType", contract.getSaleType()), sdf2.format(contract.getSaleDate()), sdf.format(contract.getBeginTime()), sdf.format(contract.getEndTime()), contract.getCardCode(), contract.getMemberCode(), contract.getMemberName(), contract.getPtCode(), this.cache.getValue("ptItemName", contract.getPtCode()), contract.getCoachCode(), this.cache.getValue("ptCoachName", contract.getCoachCode()), contract.getQuantity(), contract.getFactPrice(), contract.getPayAmount(), contract.getEarnestAmount(), this.nvl(contract.getSalesCode()) + " - " + this.nvl(this.cache.getValue("salesName", contract.getSalesCode())), this.nvl(contract.getSalesCode2()) + " - " + this.nvl(this.cache.getValue("salesName", contract.getSalesCode2())), this.nvl(contract.getCreateUser()) + " - " + this.nvl(this.cache.getValue("userName", contract.getCreateUser())), contract.getOldContractCode(), contract.getNote(), this.chkNullZ(contract.getReceivePaper())?"否":"是", this.cache.getValue("reviewStatus", String.valueOf(contract.getReviewStatus())), contract.getReviewDate() != null?sdf.format(contract.getReviewDate()):"", this.chkNullZ(contract.getEarnestDelay())?"":"是"});
            }

            xls1.close();
            this.addUserLog("导出私教销售流水", e.getResultList().size() + "条记录");
        } catch (Exception var19) {
            this.log.error("import", var19);
        }

        return null;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        List itemList = this.ptItemCodeDao.getShopItemList(this.getLoginShopCode());
        List coachList = this.ptCoachCodeDao.getShopCoachList(this.getLoginShopCode());
        ModelAndView mv = new ModelAndView("pt/contract/add");
        mv.addObject("itemList", itemList);
        mv.addObject("coachList", coachList);
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("contract") PtContract contract, @RequestParam("payls") String payls) {
        if(this.chkNull(contract.getMemberCode())) {
            return this.getResult(false, "请指定会员");
        } else if(this.chkNull(contract.getPtCode())) {
            return this.getResult(false, "请选择私教课程");
        } else if(this.chkNull(contract.getCoachCode())) {
            return this.getResult(false, "请选择上课教练");
        } else if(this.chkNull(contract.getQuantity())) {
            return this.getResult(false, "请输入数量");
        } else if(this.chkNull(contract.getFactPrice())) {
            return this.getResult(false, "请输入实际售价");
        } else {
            if(!this.chkNullZ(contract.getEarnestId())) {
                MemberEarnest saleClassify = (MemberEarnest)this.memberEarnestDao.getById(contract.getEarnestId());
                if(saleClassify == null || saleClassify.getStatus().intValue() != 0 || !saleClassify.getShopCode().equalsIgnoreCase(this.getLoginShopCode())) {
                    return this.getResult(false, "定金不可用");
                }

                contract.setEarnestAmount(saleClassify.getEarnestAmount());
            } else {
                contract.setEarnestId((Integer)null);
                contract.setEarnestAmount(Double.valueOf(0.0D));
            }

            contract.setShopCode(this.getLoginShopCode());
            contract.setSaleType("1");
            contract.setSaleDate(new Date());
            contract.setBespeakQuantity(Integer.valueOf(0));
            contract.setClearQuantity(Integer.valueOf(0));
            contract.setInAmount(Double.valueOf(0.0D));
            contract.setStatus(Integer.valueOf(1));
            contract.setCommissionDate(new Date());
            contract.setReceivePaper(Integer.valueOf(0));
            contract.setSignStatus(Integer.valueOf(this.chkNullZ(contract.getSignStatus())?2:0));
            contract.setPrintLabel(Integer.valueOf(0));
            String saleClassify1 = "2";
            Integer pn = this.ptContractDao.getCountByMemberCode(contract.getMemberCode());
            if(!this.chkNullZ(pn)) {
                saleClassify1 = "1";
            }

            Date dt = this.memberContractDao.getFirstDateByMemberCode(contract.getMemberCode());
            if(this.getAccountDate(new Date()).equals(this.getAccountDate(dt))) {
                saleClassify1 = "3";
            }

            contract.setSaleClassify(saleClassify1);
            contract.setCreateUser(this.getLoginUserCode());
            contract.setCreateTime(new Date());
            contract.setUpdateTime(new Date());
            contract.setReviewStatus(Integer.valueOf(1));
            ArrayList payList = new ArrayList();
            StringBuilder payMsg = new StringBuilder();
            PayList pay = this.getPayList(payls, payList, "02", payMsg);
            if(pay == null) {
                return this.getResult(false, payMsg.toString());
            } else if(!this.equals(pay.getAmount().doubleValue(), contract.getPayAmount().doubleValue())) {
                return this.getResult(false, "付款金额不一致");
            } else {
                contract.setSalesCode(pay.getSalesCode());
                contract.setSalesCode2(pay.getSalesCode2());

                try {
                    this.ptContractDao.addContract(contract, payList);
                    this.smsListService.addSmsList("21", contract.getSaleId().intValue());
                    this.addUserLog("新增私教合同", "合同号:" + contract.getContractCode() + ",会员:" + contract.getMemberCode());
                } catch (Exception var10) {
                    this.log.error("add contract", var10);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getSuccess(String.valueOf(contract.getSaleId()), "保存完成");
            }
        }
    }

    @RequestMapping({"getPrice"})
    @ResponseBody
    public String getPrice(@RequestParam("ptCode") String ptCode, @RequestParam("coachCode") String coachCode, @RequestParam("quantity") int quantity) {
        PtItemCode ptItem = (PtItemCode)this.ptItemCodeDao.getById(ptCode);
        PtCoachCode ptCoach = this.ptCoachCodeDao.getById(coachCode);
        if(ptItem == null) {
            return this.getResult(false, "没有这个课程");
        } else if(ptCoach == null) {
            return this.getResult(false, "没有这个教练");
        } else {
            PtPrice price = this.ptPriceDao.getByCoachLevel(ptCode, ptCoach.getCoachLevel(), quantity);
            if(price == null) {
                return this.getResult(false, "指定的数量不能购买");
            } else {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Calendar cal = Calendar.getInstance();
                Date dt1 = cal.getTime();
                cal.add(5, price.getValidDays().intValue() * quantity);
                Date dt2 = cal.getTime();
                StringBuilder sb = new StringBuilder();
                sb.append("{priceMin:").append(price.getPriceMin().doubleValue() * (double)quantity).append("").append(",priceMax:").append(price.getPriceMax().doubleValue() * (double)quantity).append("").append(",validDays:").append(price.getValidDays().intValue() * quantity).append("").append(",beginTime:\"").append(sdf.format(dt1)).append("\"").append(",endTime:\"").append(sdf.format(dt2)).append("\"").append("}");
                return this.getSuccess(sb.toString(), "");
            }
        }
    }
}
